/**
 * 
 * @project
 * @author yanwl
 * @version 1.0
 * @data 2014-08-21
 */
package com.chnbst.bstyun.core.common;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateUtil {
	/**
	 * 传入的日期上 添加月份
	 * @param date 输入的日期
	 * @param month 要添加的月份数
	 * @return 返回新的日期
	 */
	public static Date addMonth(Date date,int month){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, month);
		
		return new Date(calendar.getTime().getTime());
		
	}
	public static Timestamp getTodayTime(){
		Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());
		return timestamp;
	}
//	public static String getTodayTime(){
//		Date date = new Date(Calendar.getInstance().getTimeInMillis());
//		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		return dateFormat.format(date);
//	}
	
	public static String getToday(){
		Date date = new Date(Calendar.getInstance().getTimeInMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}
	
	/**
	 * 比较日期 
	 * @param date 输入日期
	 * @return >0 当前日期大于今天日期 =0 当前日期等于今天日期 <0 当前日期小于今天日期
	 */
	public static int compareDate(Date date){
		Date today = new Date(Calendar.getInstance().getTimeInMillis());
		return date.compareTo(today);
	}
	
	public static int getDateAndTodayDiffer(Date date){
		Date today = new Date(Calendar.getInstance().getTimeInMillis());
		long time0=date.getTime();
		long time1=today.getTime();

		return (int)(Math.ceil((time0-time1)*1.0/(1000*60*60*24)));
	}
	
	/**
	 * 获取两个日期之间差的天数 
	 * @param startTime 起始日期    endDate  截止日期
	 * @return 天数
	 */
	public static int getDateDiffer(java.util.Date startTime,java.util.Date endTime){
		long time0 = startTime.getTime();
		long time1 = endTime.getTime();
		
		return (int)(Math.ceil((time1-time0)*1.0/(1000*60*60*24)));
	}
	
	public static String getMonth(String getType,java.util.Date startTime,java.util.Date endTime) throws ParseException{
		String startTimeStr = SignUtil.time2str(startTime, "yyyy-MM-dd");
		String endTimeStr = SignUtil.time2str(endTime, "yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar bef = Calendar.getInstance();
		Calendar aft = Calendar.getInstance();
		bef.setTime(sdf.parse(startTimeStr));
		aft.setTime(sdf.parse(endTimeStr));

		int startYear = bef.get(Calendar.YEAR);
		int endYear = aft.get(Calendar.YEAR);
		
		int startMonth = bef.get(Calendar.MONTH);
		int endMonth = aft.get(Calendar.MONTH);
		
		int startDay = bef.get(Calendar.DATE);
		int endtDay = aft.get(Calendar.DATE);
		
		int yearSpace = endYear - startYear;
		int monthSpace = endMonth - startMonth;
		int daySpace = endtDay - startDay;
		if(yearSpace > 0) {
			if(monthSpace < 0) {
				yearSpace -= 1;
				monthSpace += 12;
			}
		}
		
		if(daySpace < 0) {
			if(monthSpace > 0) {
				if(monthSpace == 1 && yearSpace == 0) {
					daySpace =  getDateDiffer(startTime,endTime);
				}else {
					daySpace += 30;
				}
				monthSpace -= 1;
			}else if(monthSpace == 0){
				if(yearSpace > 0) {
					yearSpace -= 1;
					monthSpace += 11;
					daySpace += 30;
				}
			}
		}
		
		//获取使用时间
		StringBuffer overTime = new StringBuffer();
		if(yearSpace > 0) {
			overTime.append(yearSpace+"年");
			if(monthSpace > 0) {
				overTime.append(monthSpace+"月");
			}
			if(daySpace > 0) {
				overTime.append(daySpace+"天");
			}
		}else {
			if(monthSpace > 0) {
				overTime.append(monthSpace+"月");
			}
			if(daySpace > 0) {
				overTime.append(daySpace+"天");
			}
		}
		
		//获取使用时间
		int months = yearSpace*12 + monthSpace;
		if(daySpace > 20) {
			months += 1;
		}
		
		String result = "";
		if("getOverTime".equals(getType)) {
			result = overTime.toString();
		}else {
			result = String.valueOf(months);
		}
		
		return result;
	}
	
	public static void main(String[] args) throws ParseException {
		/*long day = getDateAndTodayDiffer(Date.valueOf("2012-10-25"));
		System.out.println(day);*/
		System.out.println(new java.util.Date());
		String str = getMonth("getOverTime",new java.util.Date(),SignUtil.str2Date("2015-03-03 00:00:00", "yyyy-MM-dd"));
		if(SignUtil.isEmpty(str)) {
			System.out.println("1天");
		}
		System.out.println("结果："+str);
	}
}
